package com.modules.system.controller;

import com.common.base.BaseController;
import com.common.constant.Constants;
import com.common.util.ExcelUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import com.common.base.PageBean;
import com.common.base.PageResult;
import com.common.base.Return;
import com.common.util.BeanUtil;
import com.modules.system.entity.SysRoleEntity;
import com.modules.system.entity.SysUserEntity;
import com.modules.system.service.SysUserRoleService;
import com.modules.system.service.SysUserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 系统用户
 *
 */
@RestController
@RequestMapping("/sys/user")
@Api("swaggerTestController相关api")
public class UserController extends BaseController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserRoleService sysUserRoleService;



	/**
	 * 所有用户列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@RequiresPermissions("sys:user:list")
	@ApiOperation(value = "根据id查询学生信息", notes = "查询数据库中某个的学生信息")
	public Return list(@RequestParam Map<String, Object> params){
		//查询列表数据
		PageBean get = new PageBean(params);
		Page<SysRoleEntity> page = PageHelper.startPage(get.getPageIndex(),get.getPageSize());
		String username = BeanUtil.getMapStringValue(params, "username");
		sysUserService.getList(username);
		return Return.success().put( PageResult.setResult(page));
	}

	/**
	 * 获取登录的用户信息
	 */
	@RequestMapping(value = "/info", method = RequestMethod.GET)
	public Return info(){
		return Return.success().put(getUserPrincipal());
	}

	/**
	 * 获取登录的用户信息
	 */
	@RequestMapping(value = "/permissions", method = RequestMethod.GET)
	public Return permissions(){
		if(null == getUserPrincipal().getPermsSet()) {
			Set<String> permsSet = sysUserService.getUserPermissions(getUserPrincipal().getUserId());
			getUserPrincipal().setPermsSet(permsSet);
			return Return.success().put(permsSet);
		}else {
			return Return.success().put(getUserPrincipal().getPermsSet());
		}
	}

	/**
	 * 用户信息
	 */
	@RequestMapping(value = "/info/{userId}", method = RequestMethod.GET)
	@RequiresPermissions("sys:user:info")
	public Return info(@PathVariable("userId") Long userId){
		SysUserEntity user = sysUserService.getObject(userId);

		//获取用户所属的角色列表
		List<Long> roleIdList = sysUserRoleService.getRoleIdList(userId);
		user.setRoleIdList(roleIdList);

		return Return.success().put(user);
	}

	/**
	 * 保存用户
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@RequiresPermissions("sys:user:save")
	public Return save(@RequestBody SysUserEntity user){
		sysUserService.save(user);

		return Return.success();
	}

	/**
	 * 修改用户
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@RequiresPermissions("sys:user:update")
	public Return update(@RequestBody SysUserEntity user){
		sysUserService.update(user);

		return Return.success();
	}

	/**
	 * 删除用户
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	@RequiresPermissions("sys:user:delete")
	public Return delete(@RequestBody Long[] userIds){
		if(ArrayUtils.contains(userIds, Constants.SUPER_ADMIN)){
			return Return.error("系统管理员不能删除");
		}
		if(ArrayUtils.contains(userIds, getUserId())){
			return Return.error("当前用户不能删除");
		}
		sysUserService.deleteBatch(userIds);
		return Return.success();
	}

	/**
	 * 导出
	 */
	@RequestMapping("/export")
//	@RequiresPermissions("sys:user:export")
	public void export(HttpServletRequest request, HttpServletResponse response){

		String[] excelHeader = { "ID","国家", "商品编号", "名称","分类","创建日期"};
		String[] ds_titles = { "id","country", "sn", "name","productCategory","createdDate"};
		int[] ds_format = { 1,1, 1, 1,1,1};
		try{
			List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
			for(int i = 0; i <10; i++){
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("id", 1);
				map.put("country", "aaaaa");
				map.put("sn", 3);
				map.put("name", 4);
				map.put("productCategory", 5);
				map.put("createdDate", 6);
				data.add(map);
			}
			ExcelUtils.export("商品表", "商品", excelHeader, ds_titles, ds_format, null, data, request, response);
		}catch(Exception e){
			e.printStackTrace();
		}
	}


	/**
	 * 修改登录用户密码
	 */
//	@RequestMapping("/password")
//	public Return password(String password, String newPassword){
//
//		//原密码
//		password = ShiroUtils.sha256(password, getUser().getSalt());
//		//新密码
//		newPassword = ShiroUtils.sha256(newPassword, getUser().getSalt());
//
//		//更新密码
//		int count = sysUserService.updatePassword(getUserId(), password, newPassword);
//		if(count == 0){
//			return R.error("原密码不正确");
//		}
//
//		return Return.success();
//	}
}
